package com.qf.lanzhou.blog.api.dao.Impl.login;

import com.qf.lanzhou.blog.api.common.Common;
import com.qf.lanzhou.blog.api.dao.AuditDao;
import com.qf.lanzhou.blog.api.pojo.entity.Audit;
import com.qf.lanzhou.blog.api.pojo.entity.User;
import com.qf.lanzhou.blog.api.util.BeanProcessor;
import com.qf.lanzhou.blog.api.util.DruidUtil;
import com.qf.lanzhou.blog.api.util.MD5Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LEON-HE
 */
public class AuditDaoImpl implements AuditDao {
    private QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());

    @Override
    public Boolean selectAuditByUserId(Long userId) {
        String auditContent = Common.AUDIT_CHANGEPASSWORD_SAVE;
        Object[] inner = {userId, auditContent};
        String sql = "SELECT * FROM t_audit WHERE user_id=? and content=? and status='未处理';";
        BeanHandler<Audit> handlerAudit = BeanProcessor.getBeanHandler(Audit.class);
        try {
            Audit a = queryRunner.query(sql, handlerAudit, inner);
            if (a == null || "".equals(a)) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Boolean saveAuditChangePassword(Long userId) {
        String auditContent = Common.AUDIT_CHANGEPASSWORD_SAVE;
        Object[] inner = {auditContent, userId};
        String sql = "INSERT INTO t_audit (content,user_id) VALUES(?,?);";
        BeanHandler<Audit> handlerAudit = BeanProcessor.getBeanHandler(Audit.class);
        try {
            queryRunner.insert(sql, handlerAudit, inner);
            return true;
        } catch (RuntimeException | SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<Audit> selectAll() {
        String sql = "SELECT a.id,a.content,a.send_time,u.user_name,a.comment_id,a.status,a.back3 FROM " +
                "t_audit a LEFT JOIN t_user u on a.user_id= u.id;";
        List<Audit> audits = new ArrayList<>();
        BeanListHandler<Audit> handler = BeanProcessor.getBeanListHandler(Audit.class);
        try {
            audits = queryRunner.query(sql, handler);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return audits;
    }

    @Override
    public Boolean saveAuditReportComment(Long commentId) {
        String auditContent = Common.AUDIT_REPORTCOMMENT_SAVE;
        Object[] inner = {auditContent, commentId};
        String sql = "INSERT INTO t_audit (content,comment_id) VALUES(?,?);";
        BeanHandler<Audit> handlerAudit = BeanProcessor.getBeanHandler(Audit.class);
        try {
            queryRunner.insert(sql, handlerAudit, inner);
            return true;
        } catch (RuntimeException | SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Boolean checkAuditReportComment(Long commentId) {
        return null;
    }

    @Override
    public Boolean selectAuditByCommentId(Long commentId) {
        String auditContent = Common.AUDIT_REPORTCOMMENT_SAVE;
        Object[] inner = {commentId, auditContent};
        String sql = "SELECT * FROM t_audit WHERE comment_id=? and content=?;";
        BeanHandler<Audit> commentAudit = BeanProcessor.getBeanHandler(Audit.class);
        try {
            Audit auditComment = queryRunner.query(sql, commentAudit, inner);
            if (auditComment == null || "".equals(auditComment)) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Boolean updateInitPasswordAudit(Long auditId,Long userId) {
        Connection conn = null;
        try {
            conn = DruidUtil.getDataSource().getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String pass = MD5Utils.stringToMD5(Common.AUDIT_PROCESSING_INIT_PASSWORD);
        String update_sql = "UPDATE t_audit SET `status` = '已处理' WHERE id=?;";
        System.out.println(pass);
        System.out.println(userId);
        String init_sql = "UPDATE t_user SET `password` = ? WHERE id=?;";
        Object[] initInner = {pass,userId};
        try {
            conn.setAutoCommit(false);
            queryRunner.update(conn,update_sql,auditId);
            queryRunner.update(conn,init_sql,initInner);
            conn.commit();
            conn.close();
            return true;
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Boolean updateFailAudit(Long auditId) {
        String sql = "UPDATE t_audit SET `status` = '已处理' WHERE id=?;";
        try {
            queryRunner.update(sql,auditId);
            return true;
        } catch (RuntimeException | SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Long selectUserIdByAuditId(Long auditId) {
        String sql = "SELECT user_id FROM t_audit WHERE id=?;";
        Long query = null;
        try {
            query = queryRunner.query(sql,new ScalarHandler<Long>(),auditId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public Boolean updateCommentByCommentId(Long commentId) {
        Object[] obj={commentId};
        String sql1="UPDATE t_audit SET `status`='已处理' WHERE comment_id=?;";
        String sql2="UPDATE t_comment SET `comment_content`='评论已被举报' WHERE comment_id=?;";
        try {
            queryRunner.update(sql1,obj);
            queryRunner.update(sql2,obj);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<Audit> selectPersonAll(Long uid) {
        String sql = "SELECT a.id,a.content,a.send_time,u.user_name,a.comment_id,a.status,a.back3 FROM " +
                "t_audit a LEFT JOIN t_user u on a.user_id= u.id where user_id=?";
        List<Audit> audits = new ArrayList<>();
        BeanListHandler<Audit> handler = BeanProcessor.getBeanListHandler(Audit.class);
        try {
            audits = queryRunner.query(sql, handler,uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return audits;
    }
}
